package adminLoginPack;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ImmovableIndia.ConnectionPool;



public class CommonAdminPrevilegesAction extends Action {

	  ConnectionPool pool=null;
	  public ActionForward execute(ActionMapping mapping, ActionForm inForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		 
		  Connection con=null;
		  Statement st=null;
		  ResultSet rs = null;
		  boolean duplicate = false;
		  try{
			  String CircularNumber = request.getParameter("CircularNumber");
			  String CircularSubject = request.getParameter("CircularSubject");
			  String masterOrNot = request.getParameter("masterOrNot");
			  String id = request.getParameter("methodid");
			  int Services = 0;
			  if( null != request.getParameter("Services") && !"undefined".equals(request.getParameter("Services"))){
			  Services = Integer.parseInt(request.getParameter("Services"));
			  }
			  //ResultSet rs = null;
			  int isUpdate = 0;
			  con=getConnection();
				st=con.createStatement();
				
				Date now = new Date();
				String pattern = "yyyy-MM-dd";
				//2002-04-24
				SimpleDateFormat formatter = new SimpleDateFormat(pattern);

				String mysqlDateString = formatter.format(now);
				
				if("Circulars".equals(id)){
					
					
					 CircularNumber = request.getParameter("ArticlesNumber");
					 CircularSubject = request.getParameter("articlesSubjectMatter");
					 
					 String DOA = request.getParameter("DOA");
					 
					 String dateofNotification1 = DateFormatTest(DOA);
					// ResultSet rs = null;
					 
					rs = st.executeQuery("select TOPIC from circulars where TOPIC= "+CircularNumber+" and date_of_circular = '"+dateofNotification1+"'");
					
					boolean flag = false;
					while(rs.next()){
						flag= true;
					}
					
					if(!flag){
						isUpdate = st.executeUpdate("insert  into circulars(`Service_no`,`TOPIC`," +
								"`circular_subject`,`Created_By`,`Created_On`,`master`,`date_of_circular`)"+
								" values" + 
								" ("+Services+",'"+CircularNumber+"','"+CircularSubject+"','admin','"+mysqlDateString+"','N','"+dateofNotification1+"')");
						
						 if("Y".equals(masterOrNot)){
							  id="Master Circular";
						  }
					}else{
						duplicate =  true;
					}
					
					
				}else if("Articles".equals(id)){

					 String ArticlesNumber = request.getParameter("ArticlesNumber");
					 String articlesSubjectMatter = request.getParameter("articlesSubjectMatter");
					 
					 String DOA = request.getParameter("DOA");
					 
					 String dateofNotification1 = DateFormatTest(DOA);
					 

					isUpdate = st.executeUpdate("insert  into `articles`(`Service_no`,`articlesSubjectMatter`,`CreatedOn`,`ArticlesNumber`" +
									",`date_of_article`,`CreatedBy`)"+
									" values" + 
									" ('"+Services+"','"+articlesSubjectMatter+"','"+mysqlDateString+"','"+ArticlesNumber+"','"+dateofNotification1+"','Admin')");
					
				}else if("Notifications".equals(id)){

					 String notificationNumber = request.getParameter("NotificationNumber");
					 String dateofNotification = request.getParameter("DateofNotification");
					 java.util.Date utilDate = new java.util.Date();
					 java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
					   
					 String dateofNotification1 = DateFormatTest(dateofNotification);
					 String subjectMatter = request.getParameter("SubjectMatter");
					 String effectivedate = request.getParameter("Effectivedate");
					 String dateofNotification2 = DateFormatTest(effectivedate);
					    
					isUpdate = st.executeUpdate("insert  into notification(`serviceno`," +
							"`Notification_Number`,`Date_of_Notification`,`Subject_Matter`,"+
							"`Effective_date`,`modifiedBy`,`modifiedOn`,`createdBy`,`createdon`)"+
							" values" + 
							" ("+Services+",'"+notificationNumber+"','"+dateofNotification1+"','"+subjectMatter+"','"+dateofNotification2+"','admin','"+mysqlDateString+"','admin','"+mysqlDateString+"')");
					
				}else if("PressNotes".equals(id)){

					  String Slnumber = null != request.getParameter("Slnumber")?(String)request.getParameter("Slnumber"):"";
					  String Subject = null != request.getParameter("Subject")?(String)request.getParameter("Subject"):"";
					  String Notes = null != request.getParameter("Notes")?request.getParameter("Notes"):"";
					  String date1 = null != request.getParameter("Date1")?request.getParameter("Date1"):"";
					  
					  
					  String dateofNotification1 = DateFormatTest(date1);
					  String EFD = DateFormatTest(null != request.getParameter("EFD")?request.getParameter("EFD"):"");
					  
					isUpdate =st.executeUpdate("insert  into `pressnotes`(`ServiceNum`,`Slumber`,`Subject`,`Notes`,`CreatedBy`,`CreatedOn`,`Normal_date`,`Effective_date`) "+
											" values" + 
											" ("+Services+","+Slnumber+",'"+Subject+"','"+Notes+"','admin','"+mysqlDateString+"','"+dateofNotification1+"','"+EFD+"')");
					
				}else if("faq".equals(id)){

					rs = st.executeQuery("select max(Question_Number) from FAQ");
					
					BigDecimal questionNume = null;
					while(rs.next()){
						questionNume = rs.getBigDecimal(1);
					}
					
					if(null == questionNume){
						questionNume = new BigDecimal("1");
					}else{
						
						questionNume = new BigDecimal(questionNume.intValue()+1);
					}
					
					String question = null != request.getParameter("Question")?request.getParameter("Question"):"";
					String Question_number = null != request.getParameter("Question_number")?request.getParameter("Question_number"):"";
					String answer = null != request.getParameter("Answer")?request.getParameter("Answer"):"";
					 
					isUpdate =st.executeUpdate("insert  into FAQ(Question_Number,`Question`," +
									"`Answer`,`CreatedBy`,`CreatedOn`,service_no)"+
									" values" + 
									" ("+questionNume+",'"+question+"','"+answer+"','admin','"+mysqlDateString+"',"+Services+")");
					
				}else if("Definitions".equals(id)){
					
					
					 String RNo = null != request.getParameter("RNo")?(String)request.getParameter("RNo"):"";
					  String ARR = null != request.getParameter("ARR")?(String)request.getParameter("ARR"):"";
					  String caption = null != request.getParameter("caption")?request.getParameter("caption"):"";
					  String Definition = null != request.getParameter("Definition")?request.getParameter("Definition"):"";
					  
					  
					 
						
						
						isUpdate = st.executeUpdate("insert  into Definitions(`Service_no`,`Section_Rule`," +
								"`act_rule`,`Caption`,`definition`,`created_by`,`created_on`)"+
								" values" + 
								" ("+Services+",'"+RNo+"','"+ARR+"','"+caption+"','"+Definition+"','admin','"+mysqlDateString+"')");
						
						 if("Y".equals(masterOrNot)){
							  id="Master Circular";
						  }
						
					}else if("updateCirculars121".equals(id)){
						
						 String DOA = request.getParameter("DOA");
						 
						 String dateofNotification1 = DateFormatTest(DOA);
						 
						 String CircularNumber1 = null != request.getParameter("ArticlesNumber")?request.getParameter("ArticlesNumber"):"";
						 
						 String[] circularNumAndDate = (String[])CircularNumber1.split("~~");
						 
						CircularSubject = null != request.getParameter("articlesSubjectMatter")?request.getParameter("articlesSubjectMatter"):"";
						
						//masterOrNot = "N";
						CircularNumber =  null != request.getParameter("Services")?request.getParameter("Services"):"";
						
						isUpdate = st.executeUpdate("update  circulars set circular_subject='"+CircularSubject+"',master='"+masterOrNot+"' where Service_no="+Services+" and TOPIC='"+circularNumAndDate[0]+"' and Date_of_circular='"+dateofNotification1+"'");
								
						id = "Circulars";
					}else if("updateDefinitions121".equals(id)){
						String actRule  = null != request.getParameter("actRule")?request.getParameter("actRule"):"";
						String definitions  = null != request.getParameter("definitions")?request.getParameter("definitions"):"";
						String PrevousDefi  = null != request.getParameter("PreviousDeff")?request.getParameter("PreviousDeff"):"";
						String service[] = PrevousDefi.split("~~");
						 String CircularNumber1 = null != request.getParameter("ArticlesNumber")?request.getParameter("ArticlesNumber"):"";
						 String[] circularNumAndDate = (String[])CircularNumber1.split("~~");
						CircularSubject = null != request.getParameter("articlesSubjectMatter")?request.getParameter("articlesSubjectMatter"):"";
						CircularNumber =  null != request.getParameter("Services")?request.getParameter("Services"):"";
						isUpdate = st.executeUpdate("update  definitions set act_rule='"+actRule+"' ,definition='"+definitions+"' where Service_no="+Services+" and serialnumber='"+service[0]+"' and act_rule ='"+service[1]+"'");
						id = "Definition";
						
					}else if("updateNotifications121".equals(id)){
						
						String serviceNumber = request.getParameter("serviceNumber");
						String subjectMatter = request.getParameter("subjectMatter");
						String notificationsNumber = request.getParameter("notificationsNumber");
						String dateofNotification = "";
						if(null != request.getParameter("dateofNotification") && !"".equals(request.getParameter("dateofNotification"))){
							dateofNotification = DateFormatTest(request.getParameter("dateofNotification"));	
						}
						
						String effectiveDate = "";
						if(null != request.getParameter("dateofNotification") && !"".equals(request.getParameter("dateofNotification"))){
							effectiveDate = DateFormatTest(request.getParameter("effectiveDate"));	
						}
						
						isUpdate = st.executeUpdate("update  notification set Subject_Matter='"+subjectMatter+"',Date_of_Notification='"+dateofNotification+"', Effective_date ='"+effectiveDate+"' where serviceNo="+Services+" and Notification_Number="+notificationsNumber+"");
								
						id = "notification";
					}else if("updateArticles121".equals(id)){
						
						String serviceNumber = request.getParameter("Services");
						String subjectMatter = request.getParameter("subjectMatter");
						String notificationsNumber = request.getParameter("notificationsNumber");
						
						String effectiveDate = "";
						if(null != request.getParameter("effectiveDate") && !"".equals(request.getParameter("effectiveDate"))){
							effectiveDate = DateFormatTest(request.getParameter("effectiveDate"));	
						}
						
						isUpdate = st.executeUpdate("update  articles set articlesSubjectMatter='"+subjectMatter+"', date_of_article ='"+effectiveDate+"' where ServiceNum="+Services+" and ArticlesNumber="+notificationsNumber+"");
								
						id = "articles";
					}else if("updateFAQ".equals(id)){

						String serviceNumber = null != request.getParameter("Services")?request.getParameter("Services"):"";
						String questionNUmber = null != request.getParameter("questionNumber")?request.getParameter("questionNumber"):"";
						String question = null != request.getParameter("question")?request.getParameter("question"):"";
						String answer = null != request.getParameter("answer")?request.getParameter("answer"):"";
						
						
						
						
						isUpdate = st.executeUpdate("update FAQ set question='"+question+"', answer ='"+answer+"' where Service_no="+Services+" and Question_number="+questionNUmber);
								
						id = "FAQ";
					}

				
				if(isUpdate == 1 || isUpdate == 2){
					response.getWriter().write("<font style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px' color='blue' size='12' >"+id+" updated.");
				}
				
				if(duplicate){
					response.getWriter().write("<font style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px' color='red' size='12' >your inserted record already exists with the same number and date.");
				}
				
		  }catch(Exception e){
			  e.printStackTrace();
		  }finally{
			  try {
					if(null != rs)
						pool.releaseResources(rs);
					System.out.println("Connection RS closed........--------------");
					if(null != st)
						pool.releaseResources(st);
					System.out.println("Connection ST closed........--------------");
					if(null != con)
					pool.releaseConnection(con);
					System.out.println("Connection con closed........--------------");
				} catch (Exception e) {
					e.printStackTrace();
				}
		  }
		   
		  return null;
	    
	  }
	  
	  public String DateFormatTest(String date) 
	  {
		  String converted_date = "";
		  try{
		  converted_date = converted_date + date.substring(date.lastIndexOf('/')+1,date.length()); 
		  converted_date= converted_date +"-"+date.substring(0,date.indexOf('/')); 
		  converted_date = converted_date+"-"+date.substring(date.indexOf('/')+1,date.lastIndexOf('/'));
	  
	  System.out.print(converted_date); 
	  }catch(Exception e){
		  e.printStackTrace();
	  }
	  return converted_date;
	  
} 
	  public Connection getConnection() throws SQLException, Exception {
			pool=new ConnectionPool();
			return pool.getConnection();
	}
	  
}
